package com.example.myapplication.ui.merchant;

import android.content.Intent;
import android.view.View;

import androidx.recyclerview.widget.GridLayoutManager;

import com.example.myapplication.R;
import com.example.myapplication.adapter.GoodsAdapter;
import com.example.myapplication.adapter.IGoodsListener;
import com.example.myapplication.bean.Goods;
import com.example.myapplication.bean.Merchant;
import com.example.myapplication.dao.MerchantDao;
import com.example.myapplication.database.GoodsDBUtils;
import com.example.myapplication.database.GreenDaoManager;
import com.example.myapplication.databinding.ActivityCommonBinding;
import com.example.myapplication.ui.user.GoodsDetailActivity;
import com.example.myapplication.widget.MyItemDecoration;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import cn.project.base.baseui.BaseNormalActivity;

public class ShopListActivity extends BaseNormalActivity<ActivityCommonBinding> implements IGoodsListener {

    Merchant merchant;
    MerchantDao merchantDao;
    GoodsAdapter goodsAdapter;
    @Override
    protected int getLayoutId() {
        return R.layout.activity_common;
    }

    @Override
    protected void initView() {
        merchant = (Merchant) getIntent().getSerializableExtra("merchant");
        merchantDao = GreenDaoManager.getmInstance().getDaoSession().getMerchantDao();

        mBinding.toolbar.rlBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        mBinding.toolbar.tvTitle.setText(merchant.getUsername());
    }

    @Override
    protected void initData() {
        goodsAdapter = new GoodsAdapter(this,ShopListActivity.this);
        mBinding.rlCommon.setLayoutManager(new GridLayoutManager(getApplicationContext(),2));
        mBinding.rlCommon.addItemDecoration(new MyItemDecoration(10));
        mBinding.rlCommon.setAdapter(goodsAdapter);
        getGoodList();
    }

    private void getGoodList() {
        List<Goods> goodsList = GoodsDBUtils.getInstance(getApplicationContext()).getGoodsList();
        List<Goods> list = new ArrayList<>();
        for (int i = 0; i < goodsList.size(); i++) {
            if (merchant.getId() == goodsList.get(i).getUserid()){
                list.add(goodsList.get(i));
            }
        }
        Collections.reverse(list);
        goodsAdapter.setNewData(list);
        goodsAdapter.notifyDataSetChanged();
    }

    @Override
    public void onGoodsClick(Goods goods) {
        Intent intent = new Intent(ShopListActivity.this, GoodsDetailActivity.class);
        intent.putExtra("goods",goods);
        startActivity(intent);
    }
}
